package adapter;

import java.util.ArrayList;

import utils.GridViewItem;

import com.example.lastprojectbeautiplus.R;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeGridViewAdapter extends ArrayAdapter<GridViewItem>{
	private ArrayList<GridViewItem> bannerList;
	private Activity activity;
	private int Resource;
	ImageView imgBanner;
	TextView tvBanner;
	View view;
	public HomeGridViewAdapter(Activity activity,int Resource, ArrayList<GridViewItem> bannerList){
		super(activity, Resource, bannerList);
		this.bannerList = bannerList;
		this.activity = activity;
		this.Resource = Resource;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bannerList.size();
	}
	public GridViewItem getItem(int position) {
        return bannerList.get(position);
    }

    public long getItemId(int position) {
        return bannerList.get(position).getImgPath();
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = convertView;
		if(view == null){
			view = inflater.inflate(Resource, null);
			imgBanner = (ImageView) view.findViewById(R.id.imgBanner);
		    tvBanner = (TextView) view.findViewById(R.id.tvBanner);
		    DisplayMetrics metrics = view.getResources().getDisplayMetrics();
		    int width;
		    width = metrics.widthPixels;
		    imgBanner.getLayoutParams().width = width/4;
		    imgBanner.getLayoutParams().height = width/4;
		    imgBanner.setImageResource(bannerList.get(position).getImgPath());
		    
		    tvBanner.setText(bannerList.get(position).getBanner());
		}
		else 
			view = (View) convertView;
		return view;
	}

}
